package com.scunewscreeper.controller;

import com.scunewscreeper.form.StarNewsForm;
import com.scunewscreeper.service.authority.AuthorityService;
import com.scunewscreeper.service.news.NewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@Api(tags = "文章接口")
@RequestMapping("/api/v1/news")
@RequiredArgsConstructor
public class NewsController {
    @Autowired
    AuthorityService authorityService;
    @Autowired
    NewsService newsService;

    @ApiOperation(value = "获取新闻列表")
    @ResponseBody
    @GetMapping(value = "get_news_list")
    public Object GetNewsList(@RequestHeader String token, @RequestParam int page,@RequestParam(required = false) String source,@RequestParam(required = false) String type){
        log.info("获取新闻列表");
        authorityService.GetUserIdByToken(token);
        return newsService.GetNewsList(page, type, source);
    }

    @ApiOperation(value = "查看新闻详情")
    @ResponseBody
    @GetMapping(value = "get_news_detail")
    public Object GetNewsDetail(@RequestHeader String token, @RequestParam String newsId){
        log.info("查看新闻详情");
        String openId=authorityService.GetUserIdByToken(token);
        return newsService.GetNewsDetail(newsId,token,openId);
    }

    @ApiOperation(value = "用户添加收藏")
    @ResponseBody
    @PostMapping(value = "star_news")
    public Object StarNews(@RequestHeader String token, @RequestBody StarNewsForm starNewsForm){
        log.info("用户添加收藏");
        String openId=authorityService.GetUserIdByToken(token);
        return newsService.StarNews(starNewsForm.getNewsId(),openId);
    }
    @ApiOperation(value = "点赞文章")
    @ResponseBody
    @PostMapping(value = "like_news")
    public Object LikeNews(@RequestHeader String token, @RequestBody StarNewsForm lieNewsForm){
        log.info("点赞文章");
        String openId=authorityService.GetUserIdByToken(token);
        return newsService.LikeNews(lieNewsForm.getNewsId());
    }
    @ApiOperation(value = "查看收藏列表")
    @ResponseBody
    @GetMapping(value = "get_star_news")
    public Object GetStarNews(@RequestHeader String token){
        log.info("查看用户个人收藏");
        String openId=authorityService.GetUserIdByToken(token);
        return newsService.GetStarNewsList(openId);
    }
    @ApiOperation(value = "查看浏览记录")
    @ResponseBody
    @GetMapping(value = "get_history_views")
    public Object GetHistoryViews(@RequestHeader String token){
        log.info("查看用户浏览记录");
        String openId=authorityService.GetUserIdByToken(token);
        return newsService.GetHistoryViews(openId);
    }
    @ApiOperation(value = "获取轮播图")
    @ResponseBody
    @GetMapping(value = "get_sticky_post")
    public Object GetStickyNews(@RequestHeader String token){
        log.info("获取轮播图");
        String openId=authorityService.GetUserIdByToken(token);
        return newsService.GetStickyNews(openId);
    }
}
